package com.linx.park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.exception.ServiceException;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.park.domain.ParkInfo;
import com.linx.park.domain.ParkPlace;
import com.linx.park.domain.bo.ParkInfoBo;
import com.linx.park.domain.bo.ParkPlaceBo;
import com.linx.park.domain.vo.ParkInfoVo;
import com.linx.park.domain.vo.ParkPlaceVo;
import com.linx.park.mapper.ParkInfoMapper;
import com.linx.park.mapper.ParkPlaceBusinessMapper;
import com.linx.park.mapper.ParkPlaceMapper;
import com.linx.park.service.IParkBuildingAreaService;
import com.linx.park.service.IParkPlaceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 园区区域Service业务层处理
 *
 * @author 泽智科技
 * @date 2024-10-09
 */
@RequiredArgsConstructor
@Service
public class ParkBuildingAreaServiceImpl implements IParkBuildingAreaService {


    private final ParkPlaceMapper baseMapper;
    @Autowired
    private ParkPlaceBusinessMapper parkPlaceBusinessMapper;
    //@Autowired
    //private ParkInfoMapper parkInfoMapper;

    /**
     * 查询园区区域
     *
     * @param parkPlaceId 主键
     * @return 园区区域
     */
    @Override
    public ParkPlaceVo queryById(Long parkPlaceId){
        return baseMapper.selectVoById(parkPlaceId);
    }

    /**
     * 分页查询园区区域列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 园区区域分页列表
     */
    @Override
    public TableDataInfo<ParkPlaceVo> queryPageList(ParkPlaceBo bo, PageQuery pageQuery) {
        //Page<ParkPlaceVo> parkPlaceVos = parkPlaceBusinessMapper.selectPlaceBusinessList(bo,pageQuery.build());
        LambdaQueryWrapper<ParkPlace> lqw = buildQueryWrapper(bo);
        Page<ParkPlaceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的园区区域列表
     *
     * @param bo 查询条件
     * @return 园区区域列表
     */
    @Override
    public List<ParkPlaceVo> queryList(ParkPlaceBo bo) {
        LambdaQueryWrapper<ParkPlace> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ParkPlace> buildQueryWrapper(ParkPlaceBo bo) {
        LambdaQueryWrapper<ParkPlace> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParkInfoId() != null, ParkPlace::getParkInfoId, bo.getParkInfoId());
        lqw.eq(bo.getTenantId() != null, ParkPlace::getTenantId, bo.getTenantId());
        lqw.eq(bo.getParkBuildingId() != null, ParkPlace::getParkBuildingId, bo.getParkBuildingId());
        lqw.eq(bo.getPid() != null, ParkPlace::getPid, bo.getPid());
        lqw.like(StringUtils.isNotBlank(bo.getPlaceName()), ParkPlace::getPlaceName, bo.getPlaceName());
        lqw.eq(bo.getType() != null, ParkPlace::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getParkInfoCode()), ParkPlace::getParkInfoCode, bo.getParkInfoCode());
        lqw.eq( ParkPlace::getDelFlag, "0");
        lqw.like(StringUtils.isNotBlank(bo.getParkBuildingAreaName()), ParkPlace::getParkBuildingAreaName, bo.getParkBuildingAreaName());
        return lqw;
    }

    /**
     * 新增园区区域
     *
     * @param bo 园区区域
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ParkPlaceBo bo) {
        ParkPlace add = MapstructUtils.convert(bo, ParkPlace.class);
        validEntityBeforeSave(add);
        add.setDelFlag("0");
        add.setType(Long.valueOf(2));
        add.setPid(bo.getParkBuildingId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setParkPlaceId(add.getParkPlaceId());
        }
        return flag;
    }

    /**
     * 修改园区区域
     *
     * @param bo 园区区域
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ParkPlaceBo bo) {
        ParkPlace update = MapstructUtils.convert(bo, ParkPlace.class);
        validEntityBeforeSave(update);

        UpdateWrapper<ParkPlace> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("park_building_area_id", bo.getParkPlaceId()) // 设置更新条件，这里是id等于userId
            .set("park_building_area_name", bo.getPlaceName()); // 设置要更新的字段，这里是email字段更新为newEmail
        baseMapper.update(updateWrapper); // 执行更新操作
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ParkPlace entity){
        //TODO 做一些数据校验,如唯一约束
        //查询园区
        int num = parkPlaceBusinessMapper.selectParkPlaceInfoNumById(entity.getParkInfoId());
        if(0==num){
            throw new ServiceException("园区不存在");
        }
        //entity.setParkInfoCode(parkInfoVo.getParkInfoCode());



    }

    /**
     * 校验并批量删除园区区域信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }

        if (ids.isEmpty()) {
            throw new ServiceException("请选择需要删除的数据");
        }

        List<ParkPlaceVo> parkPlaceVos = baseMapper.selectVoBatchIds(ids, ParkPlaceVo.class);
        parkPlaceVos.forEach(x ->{
            x.setDelFlag("1");
        });
        List<ParkPlace> parkPlaceList = MapstructUtils.convert(parkPlaceVos, ParkPlace.class);
        //return baseMapper.deleteByIds(ids) > 0;
        UpdateWrapper<ParkPlace> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("park_building_area_id", ids) // 设置更新条件，这里是id等于userId
            .set("del_flag", "1"); // 设置要更新的字段，这里是email字段更新为newEmail
        baseMapper.update(updateWrapper); // 执行更新操作


        return baseMapper.updateBatchById(parkPlaceList);


        //return baseMapper.deleteByIds(ids) > 0;
    }
}
